/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;


import java.util.List;
import model.Client;
import javax.inject.Named;
import javax.enterprise.context.ConversationScoped;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.inject.Inject;
import controller.UserFacade;

/**
 * This class is the manager between all view that require user change like logging.xhtml, userRight.xhtml
 * and their controller
 * @author yann
 */
@Named(value = "userManager")
@ConversationScoped
public class UserManager implements Serializable {
    @EJB
    private UserFacade userFacade;
    @Inject
    private Conversation conversation;
    private Exception transactionFailure;
    private String name;
    private String password;
    private Client client;
    private boolean connected;
    
    /**
     * Start a conversation to store variable relative to the session
     */
    private void startConversation() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }
    
    /**
     * Stop the current conversation, used to log out the user
     */
    private void stopConversation() {
        if (!conversation.isTransient()) {
            conversation.end();
        }
    }
    
    /**
     * Get the current conversation
     * @return return the current conversation
     */
    public Conversation getConversation() {
        return conversation;
    }
    
    /**
     * Get the id of the current conversation
     * @return return the id of the current conversation
     */
    public String getIdConversation() {
        System.out.println("Conversation id :"+ conversation.getId());
        return conversation.getId();
    }
    
    /**
     * Handle the exception and set the transaction failure
     * @param e catch the exception coming from somewhere else
     */
    private void handleException(Exception e) {
        stopConversation();
        e.printStackTrace(System.err);
        transactionFailure = e;
    }
    
    /**
     * Returns the latest thrown exception.
     */
    public Exception getException() {
        return transactionFailure;
    }
    
    /**
     * Returns <code>false</code> if the latest transaction failed, otherwise <code>false</code>.
     */
    public boolean getSuccess() {
        return transactionFailure == null;
    }

    
    /** Creates a new instance of userManager */
    public UserManager() {
        client = null;
        connected = false;
    }

    /**
     * Get the name of the user
     * @return name user
     */
    public String getName() {
        return name;
    }

    /**
     * Set name of user
     * @param name      user Name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Get password user
     * @return password user
     */
    public String getPassword() {
        return password;
    }

    /**
     * Set user password
     * @param password user password
     */
    public void setPassword(String password) {
        this.password = password;
    }
    /**
     * Get the client once the user is connected
     * @return 
     */
    public Client getClient() {
        return client;
    }
    /**
     * To know if the user is connected or not
     * @return 
     */
    public boolean isConnected() {
        return connected;
    }
    
    /**
     * Try to log the user with name and password enter previously
     */
    public void logging(){
        try {
            startConversation();
            client = userFacade.logging(name, password);
            connected = true;
            System.out.println("Conversation id :"+ conversation.getId());
        } catch (Exception ex) {
            handleException(ex);
        }
    }
    
    /**
     * Try to create a new user
     */
    public void createUser(){
        try {
            startConversation();
            client = userFacade.createUser(name, password);
            connected = true;
        } catch (Exception ex) {
            handleException(ex);
        }
    }
    
    /**
     * Logout the current user
     */
    public void logOut(){
        stopConversation();
        connected = false;
    }
    /**
     * Get a list of all client register in the database
     * @return return a list with all client
     */
    public List<Client> getListOfAllClient() {
        List<Client> list = userFacade.getListOfAllClient();
        return list;
    }
    
    
}
